package org.shangrila.app.sys.mb;


import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;

public final class Test implements Serializable {
	private static final long serialVersionUID = 2163916067335213382L;
	private final static String CHAR_SET = "GBK";
	private final static String command="cmd.exe /c tasklist | findstr 360";

	static List<String> execCmd(String cmdstr) throws IOException {
		Process process = null;
		try {
			final String osName = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
			final boolean windows = osName.contains("windows");
			final boolean mac = osName.contains("mac");
			final boolean aix = osName.contains("aix");
			if (windows) {
				process = Runtime.getRuntime().exec(new String[] { "cmd", "/c", cmdstr });
			} else if (mac || aix) {
				process = Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", cmdstr });
			} else {
				process = Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", cmdstr });
			}
			final String charset;
			if (windows) {
				charset = "GBK";
			} else {
				charset = "UTF-8";
			}
			final Scanner sc = new Scanner(process.getInputStream(), charset);
			sc.useRadix(10);
			sc.useLocale(Locale.US);
			sc.nextLine();
			/*
			if (windows) {
				sc.nextLine();
				sc.nextLine();
			}
			 */
			final List<String> processInfos = new ArrayList<String>();
			while (sc.hasNext()) {
				processInfos.add(sc.nextLine());
			}
			return Collections.unmodifiableList(processInfos);
		} finally {
			if (process != null) {
				process.getInputStream().close();
				process.getOutputStream().close();
				process.getErrorStream().close();
				process.destroy();
			}
		}
	}

	
	public static void main(String[] args) throws IOException {
		/*
		String[] r = runLocalCommand(command);
		System.out.println(r[0]);
		System.out.println(r[1]);
		System.out.println("----------------------------------------");
		List<String> lr2 = runLocalCommand2(command);
		for(String r2:lr2) System.out.println(r2);
		System.out.println("----------------------------------------");
		*/
		List<String> ls= execCmd(command);
		System.out.println(ls);
		//for(String p:ls)	System.out.println(p);

	}
}
